﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.JShop.UI;
using System.Data;
using CNVP.Framework;
using CNVP.Framework.Utils;

namespace CNVP.JShop.Web.Admin
{
    public partial class StandardEdit : AdminPage
    {
        BLL.CNVP_Standard bll = new BLL.CNVP_Standard();
        Model.CNVP_Standard model = new Model.CNVP_Standard();
        public int id = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!BLL.BLLCheckAdmin.LoginOk(Page, 93)) return;//判断页面权限
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]) && Public.IsNumber(Request.QueryString["ID"]))
            {
                id = Convert.ToInt32(Request.QueryString["ID"]);
            }

            if (!IsPostBack)
            {
                if (id > 0)
                {
                    lbTitle.Text = " > 修改";
                    DataInfo(id);
                    repeatBind();
                }
                else
                {
                    lbTitle.Text = " > 添加";
                }
                #region 验证
                rblStandardType.Attributes.Add("onClick", "StandardType()");
                #endregion

            }

            
        }

        public void DataInfo(int id)
        {
            model = bll.GetSingle_CNVP_StandardById(id);
            if (model != null)
            {
                txtStandardName.Text = model.StandardName;
                rblStandardType.SelectedValue = model.StandardType.ToString();
                txtStandardOtherName.Text = model.StandardOtherName;
                rblShowWay.SelectedValue = model.ShowWay.ToString();
                txtStandardDetail.Text = model.StandardDetail;
            }
        }

        public void repeatBind()
        {
            rptList.DataSource = new BLL.CNVP_StandardValue().GetAll_CNVP_StandardValue(id);
            rptList.DataBind();
        }

        protected void btUp_Click(object sender, EventArgs e)
        {
            if (txtStandardName.Text == "")
            {
                BasePage.MessageBox("规格名称不能为空！");
                return;
            }
            if (id > 0)
            {
                #region 修改
                int rows = new BLL.CNVP_Standard().Update_CNVP_Standard(new Model.CNVP_Standard()
                {
                    ID = id,
                    StandardName = txtStandardName.Text.Trim(),
                    ShowWay = byte.Parse(rblShowWay.SelectedValue),
                    StandardOtherName = txtStandardOtherName.Text.Trim(),
                    StandardType = byte.Parse(rblStandardType.SelectedValue),
                    StandardDetail = txtStandardDetail.Text.Trim()
                });
                if (rows == 1)
                {
                    string nowid = "";//存储当前ID
                    try
                    {
                        string StandardValueName_Str = "";
                        string StandardValueOtherName_Str = "";
                        int OrderID_Int = 0;
                        string StandardValuePic_Str = "";
                        string[] CKStandard = Request.Form["CKStandard"].Split(Convert.ToChar(","));
                        for (int i = 0; i < CKStandard.Length; i++)//注意从‘1’开始
                        { 
                            string[] valuename = Request.Form["StandardValueName"].Split(Convert.ToChar(","));
                            StandardValueName_Str = valuename[i].ToString();
                            string[] valueothername = Request.Form["StandardValueOtherName"].Split(Convert.ToChar(","));
                            StandardValueOtherName_Str = valueothername[i].ToString();
                            string[] valueOrderID = Request.Form["OrderID"].Split(Convert.ToChar(","));
                            try
                            {
                                OrderID_Int = int.Parse(valueOrderID[i].ToString());
                            }
                            catch { OrderID_Int = 0; }//注意这句
                            string[] valueStandardValuePic = Request.Form["StandardValuePic"].Split(Convert.ToChar(","));
                            StandardValuePic_Str = valueStandardValuePic[i].ToString();
                            int CKid = 0;
                            //string xx = CKStandard[i].ToString();
                            if (CKStandard[i].ToString() != "on")//注意不空
                            {
                                CKid = int.Parse(CKStandard[i].ToString());
                            }
                            if (CKid > 0)
                            {

                                int StandardValue_Int = new BLL.CNVP_StandardValue().Update_CNVP_StandardValue(new Model.CNVP_StandardValue()
                                {
                                    ID=CKid,
                                    StandardID = id,
                                    StandardValueName = StandardValueName_Str,
                                    StandardValueOtherName = StandardValueOtherName_Str,
                                    OrderID = OrderID_Int,
                                    StandardValuePic = StandardValuePic_Str
                                });
                                nowid += CKid + ",";
                            }
                            else
                            {
                                int StandardValue_Int = new BLL.CNVP_StandardValue().Add_CNVP_StandardValue(new Model.CNVP_StandardValue()
                                {
                                    StandardID = id,
                                    StandardValueName = StandardValueName_Str,
                                    StandardValueOtherName = StandardValueOtherName_Str,
                                    OrderID = OrderID_Int,
                                    StandardValuePic = StandardValuePic_Str
                                });
                                nowid += StandardValue_Int + ",";
                            }
                        }
                    }
                    catch{ }
                    //删除不在当前的ID
                    if (nowid.Length > 0)
                    {
                        nowid = nowid.Substring(0, nowid.Length - 1);
                    }
                    else
                    {
                        nowid = "0";
                    }
                    var ProductsDetail_List = new BLL.CNVP_ProductsDetail().GetAll_CNVP_ProductsDetail();
                    var StandardValueLoadNotIn_List = new BLL.CNVP_StandardValue().StandardValueLoadNotIn(id, nowid);
                    foreach (Model.CNVP_StandardValue s in StandardValueLoadNotIn_List)
                    {
                        foreach (Model.CNVP_ProductsDetail p in ProductsDetail_List)
                        {
                            string[] pProductsDetail = p.StandardValueIDs.Split(Convert.ToChar(","));
                            for (int k = 0; k < pProductsDetail.Length; k++)
                            {
                                if (pProductsDetail[k] == s.ID.ToString())
                                {
                                    nowid += "," + s.ID.ToString();
                                    BasePage.MessageBox("" + s.StandardValueName + "-规格值存在产品详细中,不允许删除.！");
                                }
                            }
                        }
                    }
                    new BLL.CNVP_StandardValue().Delete_CNVP_StandardValueNotIn(id, nowid);
                    BasePage.MessageBox("修改成功！", "StandardList.aspx?Page=" + Request.QueryString["Page"] + "");
                    repeatBind();
                }
                else
                { BasePage.MessageBox("修改失败！"); }
                #endregion
                
            }
            else
            {
                #region 添加
                int rows = new BLL.CNVP_Standard().Add_CNVP_Standard(new Model.CNVP_Standard()
                {
                    StandardName = txtStandardName.Text.Trim(),
                    ShowWay = byte.Parse(rblShowWay.SelectedValue),
                    StandardOtherName = txtStandardOtherName.Text.Trim(),
                    StandardType = byte.Parse(rblStandardType.SelectedValue),
                    StandardDetail = txtStandardDetail.Text.Trim()
                });
                if (rows > 0 )
                {
                    try
                    {
                        string StandardValueName_Str = "";
                        string StandardValueOtherName_Str = "";
                        int OrderID_Int = 0;
                        string StandardValuePic_Str = "";
                        string[] CKStandard = Request.Form["CKStandard"].Split(Convert.ToChar(","));
                        for (int i = 0; i < CKStandard.Length; i++)//注意从‘0’开始
                        {
                            string[] valuename = Request.Form["StandardValueName"].Split(Convert.ToChar(","));
                            StandardValueName_Str = valuename[i].ToString();
                            string[] valueothername = Request.Form["StandardValueOtherName"].Split(Convert.ToChar(","));
                            StandardValueOtherName_Str = valueothername[i].ToString();
                            string[] valueOrderID = Request.Form["OrderID"].Split(Convert.ToChar(","));
                            try
                            {
                                OrderID_Int = int.Parse(valueOrderID[i].ToString());
                            }
                            catch { OrderID_Int = 0; }//注意这句
                            string[] valueStandardValuePic = Request.Form["StandardValuePic"].Split(Convert.ToChar(","));
                            StandardValuePic_Str = valueStandardValuePic[i].ToString();
                            int StandardValue_Int = new BLL.CNVP_StandardValue().Add_CNVP_StandardValue(new Model.CNVP_StandardValue()
                            {
                                StandardID = rows,
                                StandardValueName = StandardValueName_Str,
                                StandardValueOtherName = StandardValueOtherName_Str,
                                OrderID = OrderID_Int,
                                StandardValuePic = StandardValuePic_Str
                            });
                        }

                    }
                    catch { }
                    repeatBind();
                    BasePage.MessageBox("添加成功！", "StandardList.aspx");
                }
                else
                { BasePage.MessageBox("添加失败！"); }
                #endregion
            }
        }
    }
}
